<?php
require_once __DIR__ . '/../config/database.php';

function createAdminUser($conn) {
    $username = 'admin';
    $password = 'admin123';  // 你可以修改这个密码或通过其他方式生成更安全的密码
    $hashedPassword = password_hash($password, PASSWORD_DEFAULT);
    $isAdmin = 1;  // 设置为管理员

    // 检查用户是否已存在
    $checkQuery = "SELECT * FROM users WHERE username = ?";
    $stmt = $conn->prepare($checkQuery);
    $stmt->bind_param("s", $username);
    $stmt->execute();
    $result = $stmt->get_result();
    if ($result->num_rows > 0) {
        echo "管理员账户已存在，无需重复创建。";
        return;
    }

    // 创建新管理员账户
    $query = "INSERT INTO users (username, password, is_admin) VALUES (?, ?, ?)";
    $stmt = $conn->prepare($query);
    $stmt->bind_param("ssi", $username, $hashedPassword, $isAdmin);
    if ($stmt->execute()) {
        echo "管理员账户创建成功。";
    } else {
        echo "创建管理员账户失败：" . $conn->error;
    }
}

createAdminUser($conn);
?>
